package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.controller.utis.Result;
import com.itheima.domain.Book;
import com.itheima.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private IBookService bookService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result gitAll(){
        return new Result(true,bookService.list());
    }

    /**
     * 新增
     * @param book
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Book book){
        return new Result(bookService.save(book));
    }

    /**
     * 修改 根据id
     * @param book
     * @return
     */
    @PutMapping
    public Result update(@RequestBody  Book book){
        return new Result(bookService.updateById(book));
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        return new Result(bookService.removeById(id));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result gitById(@PathVariable Integer id){
        return new Result(true,bookService.getById(id));
    }

    @GetMapping("{currentPage}/{pageSize}")
    public Result getpage(@PathVariable int currentPage,@PathVariable int pageSize){
        return new Result(true,bookService.getPage(currentPage,pageSize));
    }

}
